//
//  ModelArrayView.m
//  JiaoX
//
//  Created by yinqy on 15/3/5.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ModelArrayView.h"
#import "BBSModel.h"

#define kMarginLeft [DimensUtils getScaleX:15]
#define kMarginTop [DimensUtils getScaleX:20]

@interface ModelArrayView()
{
    UIButton *preBtn;
    UIView *view;
    UIButton *currentBtn;
}

@end
@implementation ModelArrayView

-(id)initWithModelArray:(NSArray *)array selectIndex:(NSInteger)index
{
    self = [super init];
    if (self)
    {
        preBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, JXScreenH)];
        [preBtn setBackgroundColor:[UIColor blackColor]];
        [preBtn setAlpha:0];
        [preBtn addTarget:self action:@selector(preBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:preBtn];
        
        CGFloat btnWidth = (JXScreenW-kMarginLeft*3)/2;
        CGFloat btnHeight = [DimensUtils getScaleX:35];
        CGFloat HEIGHT = 0;
        view = [[UIView alloc]init];
        [view setBackgroundColor:[UIColor whiteColor]];
        for (int i=0; i<array.count; i++) {
            BBSModel *model = array[i];
            UIButton *btn = [[UIButton alloc]init];
            [btn setCornerRadius:0 borderWidth:0.5 borderColor:JXColor(51, 51, 51)];
            [btn setTitle:model.name forState:UIControlStateNormal];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
            [btn setTitleColor:JXColor(51, 51, 51) forState:UIControlStateNormal];
            [btn setTitleColor:JXThemeColor forState:UIControlStateSelected];
            [btn setTag:i+1000];
            [btn setSize:CGSizeMake(btnWidth, btnHeight)];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            int row = i/2;//行
            int col = i%2;//列
            
            [btn setX:(col+1)*kMarginLeft+col*btn.width];
            [btn setY:(int)row*btnHeight+(row+1)*kMarginTop];
            [view addSubview:btn];
            
            if (index==i) {
                [btn setSelected:YES];
                [btn setCornerRadius:0 borderWidth:0.5 borderColor:JXThemeColor];
                currentBtn = btn;
            }
            
            HEIGHT = CGRectGetMaxY(btn.frame)+20;
        }
        
        [view setFrame:CGRectMake(0, JXScreenH, JXScreenW, HEIGHT)];
        
        [self addSubview:view];
    }
    return self;
}

-(void)btnClick:(UIButton *)btn{
    if (btn!=currentBtn) {
        [btn setSelected:YES];
        [btn setCornerRadius:0 borderWidth:0.5 borderColor:JXThemeColor];
        [currentBtn setSelected:NO];
        [currentBtn setCornerRadius:0 borderWidth:0.5 borderColor:JXColor(51, 51, 51)];
        currentBtn = btn;
    }
    if ([self.delegate respondsToSelector:@selector(modelBtnClickWithIndex:)]) {
        [self.delegate modelBtnClickWithIndex:btn.tag-1000];
    }
    [self dismiss];
}

-(void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        [preBtn setAlpha:0.3];
        [view setY:JXScreenH-view.height];
    }];
}

-(void)preBtnClick:(UIButton *)btn{
    [self dismiss];
}

-(void)dismiss{
    [UIView animateWithDuration:0.3 animations:^{
        [preBtn setAlpha:0.0];
        [view setY:JXScreenH];
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
